package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


import conexao.ConnectionManager;

import modelo.Cd;
import modelo.Musico;

public class MusicoDAOImp implements MusicoDAO{
	

	private final String INSERT = "insert into musico (nome, ehSolo, escalaVoz, instrumento) values (?,?,?,?)";
	private final String UPDATE = "update musico set nome =?, ehSolo =?, escalaVoz =?, instrumento =? where idMusico = ?";
	private final String SELECT = "select * from musico where idMusico = ?";
	private final String DELETE = "delete from musico where idMusico = ?";
	private final String ALL = "select idMusico,nome, ehSolo, escalaVoz, instrumento from musico";
	private final String LIKE = "select idMusico,nome, ehSolo, escalaVoz, instrumento from musico where nome like ?";
	
	
	private ConnectionManager connectionManager;
	private static Connection connection;
	
	MusicoDAOImp() {
		this.openConnection();
	}

	@Override
	public Musico salvar(Musico e) {
		try {
			PreparedStatement ps = null;
			//1 crie um instancia de PreparedStatement
			//para obter a chave gerada pelo banco
			ps = connection.prepareStatement(INSERT);
			
			ps.setObject(1, e.getNome());
			ps.setObject(2, e.isEhSolo());
			ps.setObject(3,e.getEscalaVoz());
			ps.setObject(4,e.getInstrumento());
			
			ps.executeUpdate();
			
		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		return e;
	}

	@Override
	public void alterar(Musico e) {
		PreparedStatement ps = null;
		try {
			//2 crie uma instancia de PreparedStatement 
			//passe para ela a instrucao para realizar alteracoes em uma registro do banco 
			ps = connection.prepareStatement(UPDATE);
						
			ps.setObject(1, e.getNome());
			ps.setObject(2, e.isEhSolo());
			ps.setObject(3,e.getEscalaVoz());
			ps.setObject(4,e.getInstrumento());
			ps.setObject(5,e.getIdMusico());
			
			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		
	}

	@Override
	public void deletar(Musico e) {
		try{
			PreparedStatement ps = null;
			ps = connection.prepareStatement(DELETE);
			//3 passe para o PreparedStatement o valor do id que deve ser excluido do banco de dados 
			ps.setObject(1, e.getIdMusico());
		
			ps.executeUpdate();
		
		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
	
	}
		
	

	@Override
	public List<Musico> findAll() {
		List<Musico> musicos = new ArrayList<Musico>();
		
		try {
			PreparedStatement ps = null;
			ResultSet rs = null;
			ps = connection.prepareStatement(ALL);
			
			//4 Obtenha uma instancia de resultset
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Musico musico = new Musico();
				musico.setIdMusico(rs.getInt("idMusico"));
				musico.setNome(rs.getString("nome"));
				musico.setEhSolo(rs.getBoolean("ehSolo"));
				musico.setEscalaVoz(rs.getInt("escalaVoz"));
				musico.setInstrumento(rs.getString("instrumento"));
				
				musicos.add(musico);
				//5 crie uma instancia de cidade com os dados
				//obtidos do banco e adicione a lista de cidades
				
			}


		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		return musicos;
	}
	

	@Override
	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	@Override
	public void openConnection() {
		if ( connectionManager == null ) {
			connectionManager = ConnectionManager.getInstance();
		}
		
		if ( connection == null) {
			connection = connectionManager.getConnection();
		}
		
		if ( isConnectionClose() ) {
			connection = connectionManager.getConnection();
		}		
	}

	@Override
	public void closeConnection() {
		if ( connection != null && connectionManager != null) {
			if ( ! this.isConnectionClose() ) {
				connectionManager.closeConnection(connection);
			}
		}		
	}

	@Override
	public List<Musico> buscaMusicoPorNome(Musico musico) {
		List<Musico> musicos = new ArrayList<Musico>();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = connection.prepareStatement(LIKE);
			//6 informe o parmetro para instrucao like
			ps.setObject(1, musico.getNome());
		
			rs = ps.executeQuery();

			while (rs.next()) {
				musicos.add(new Musico(
						rs.getInt("idMusico"),
						rs.getString("nome"),
						rs.getBoolean("ehSolo"),
						rs.getInt("escalaVoz"),
						rs.getString("instrumento")));
			}

		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		return musicos;
	}

	@Override
	public List<Musico> buscaMusicoPorId(int idMusico) {
		List<Musico> musicos = new ArrayList<Musico>();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = connection.prepareStatement(SELECT);
			//6 informe o parmetro para instrucao like
			ps.setObject(1, idMusico);
		
			rs = ps.executeQuery();

			while (rs.next()) {
				musicos.add(new Musico(
						rs.getInt("idMusico"),
						rs.getString("nome"),
						rs.getBoolean("ehSolo"),
						rs.getInt("escalaVoz"),
						rs.getString("instrumento")));
			}

		} catch (SQLException ex) {
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		return musicos;
	}

	

}
